layui.use(['table', 'admin', 'ax'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;

    /**
     * 库存流水管理
     */
    var StorageStockDetailed = {
        tableId: "storageStockDetailedTable"
    };

    /**
     * 初始化表格的列
     */
    StorageStockDetailed.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: 'ID'},
            {field: 'operation', sort: true, title: 'Transaction Type', style:'font-weight: 600;'},
            {field: 'orderNumber', title: 'Order No.', style:'color: #2b85e4;font-weight: 600;',event: 'detailedSign'},
            {field: 'type', sort: true, title: 'Type', style:'font-weight: 600;'},
            {field: 'content', title: 'Content', style:'font-weight: 600;'},
            {field: 'warehouseName', sort: true, title: 'Warehouse', style:'font-weight: 600;'},
            {field: 'areaName', sort: true, title: 'Area', style:'font-weight: 600;'},
            {field: 'positionNumber', sort: true, title: 'Location', style:'font-weight: 600;'},
            {field: 'trayNumber', sort: true, title: 'Tray', style:'font-weight: 600;'},
            {field: 'goodsName', sort: true, title: 'Product', style:'font-weight: 600;'},
            {field: 'goodsNumber', sort: true, title: 'Code', style:'font-weight: 600;'},
            {field: 'goodsNumber', sort: true, title: 'SKU', style:'font-weight: 600;'},
            {field: 'goodsUnit', title: 'Unit', style:'font-weight: 600;'},
            {field: 'goodsPacking', title: 'Packing', style:'font-weight: 600;'},
            {field: 'goodsSpecs', title: 'Specs', style:'font-weight: 600;'},
            {field: 'putTime', sort: true, title: 'Receipt Batch', style:'font-weight: 600;'},
            {field: 'goodsbatchs', sort: true, title: 'Batch', totalRowText: 'Total:', style:'font-weight: 600;'},
            {field: 'num', sort: true, title: 'Updated Qty', totalRow: true, style:'font-weight: 600;'},
            {field: 'weight', sort: true, title: 'Weight', totalRow: true, style:'font-weight: 600;'},
            {field: 'volume', sort: true, title: 'Volume', totalRow: true, style:'font-weight: 600;'},
            {field: 'money', sort: true, title: 'Amount', totalRow: true, style:'font-weight: 600;'},
            {field: 'createTime', sort: true, title: 'Time', style:'font-weight: 600;'},
            {field: 'createName', sort: true, title: 'Personnel', style:'font-weight: 600;'},
            {field: 'createAccount', sort: true, title: 'User', style:'font-weight: 600;'},
        ]];
    };

    /**
     * 点击查询按钮
     */
    StorageStockDetailed.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        table.reload(StorageStockDetailed.tableId, {
            where: queryData, page: {curr: 1},
             done:function () {
            }
        });
    };

    /**
     * 弹出添加对话框
     */
    StorageStockDetailed.openAddDlg = function () {
        window.location.href = Feng.ctxPath + '/storageStockDetailed/add';
    };

    /**
     * 导出excel按钮
     */
    StorageStockDetailed.exportExcel = function () {
        var checkRows = table.checkStatus(StorageStockDetailed.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("Please select data to export");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    StorageStockDetailed.openEditDlg = function (data) {
        window.location.href = Feng.ctxPath + '/storageStockDetailed/edit?id=' + data.id;
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    StorageStockDetailed.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/storageStockDetailed/delete", function (data) {
                Feng.success("Delete successful!");
                table.reload(StorageStockDetailed.tableId);
            }, function (data) {
                Feng.error("Delete failed! " + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("Are you sure to delete?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + StorageStockDetailed.tableId,
        url: Feng.ctxPath + '/storageStockDetailed/list?stockId='+Feng.getUrlParam("stockId"),
        page: true,
        limit: 30,
        height: "full-60",
        toolbar: '#toolbarfrom',
        cellMinWidth: 100,
        totalRow: true ,//开启合计行
        cols: StorageStockDetailed.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        StorageStockDetailed.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        StorageStockDetailed.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        StorageStockDetailed.exportExcel();
    });

    // 单击行事件
    table.on('row(' + StorageStockDetailed.tableId + ')', function (obj) {
        var data = obj.data;
		$('div[lay-id="' + StorageStockDetailed.tableId + '"]').find(".layui-table-body tr ").attr({ "style": "background:#FFFFFF" });//其他tr恢复原样
		$('div[lay-id="' + StorageStockDetailed.tableId + '"]').find(obj.tr.selector).attr({ "style": "background:#e6e6e6" });//改变当前tr颜色
    });

    // 工具条点击事件
    table.on('tool(' + StorageStockDetailed.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            StorageStockDetailed.openEditDlg(data);
        } else if (layEvent === 'delete') {
            StorageStockDetailed.onDeleteItem(data);
        } else if (layEvent === 'detailedSign') {
			if(data.operation == 'Receipt'){
	            layer.open({
	                type: 2,
	                title: 'Receipt Details',
	                area: ['1050px', '500px'],
	                anim: 5,
	                content: Feng.ctxPath + '/storageStockDetailed/toPut?storageNumber=' + data.orderNumber,
	            });
			} else if(data.operation == 'Shipment'){
	            layer.open({
	                type: 2,
	                title: 'Shipment Details',
	                area: ['1050px', '500px'],
	                anim: 5,
	                content: Feng.ctxPath + '/storageStockDetailed/toOut?storageOutNumber=' + data.orderNumber,
	            });
			}
        }
    });

});
